//
//  EditInforController.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/1.
//

import UIKit

class EditInforController: ZsBaseViewController {

    var orderModel:OrderListModel!
    typealias InformationBlock = () -> Void
    var informationBlock : InformationBlock!
    var imageInfoViewHeight:CGFloat = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_goodsMsg)
        self.view.backgroundColor = UIColor.init(hexString: "#FFFFFF")
        imageInfoViewHeight = Constants.screenWidth/4 + 10 + 20 + 10
        self.setUI()
        self.textFieldDidChange()
    }
    
    func setUI() {
        self.view.addSubview(self.scrollView)
        scrollView.addSubview(self.infoProductV)
        scrollView.addSubview(self.weightInfoView)
        scrollView.addSubview(self.imageInfoView)
        scrollView.addSubview(self.calculateTextV)
        scrollView.addSubview(self.confirmBtn)

        confirmBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.view.snp.bottom).offset(-(Constants.xSafeBottomHeight == 0 ? 8 : Constants.xSafeBottomHeight));
            make.left.equalTo(self.view.snp.left).offset(60);
            make.right.equalTo(self.view.snp.right).offset(-60);
            make.height.equalTo(40);
        }
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view.snp.top).offset(0);
            make.left.equalTo(self.view.snp.left).offset(0);
            make.width.equalTo(self.view.snp.width);
            make.height.equalTo(self.view.snp.height).offset(-(50+Constants.xSafeBottomHeight));
        }
        weightInfoView.snp.makeConstraints { (make) in
            make.width.equalTo(self.scrollView.snp.width);
            make.height.equalTo(172);
            make.top.equalTo(self.infoProductV.snp.bottom).offset(0);
        }
        imageInfoView.snp.makeConstraints { (make) in
            make.top.equalTo(self.weightInfoView.snp.bottom).offset(0);
            make.height.equalTo(self.imageInfoViewHeight);
            make.width.equalTo(Constants.screenWidth);
            make.left.equalTo(self.scrollView.snp.left).offset(0);
        }
        calculateTextV.snp.makeConstraints { (make) in
            make.width.equalTo(self.scrollView.snp.width);
            make.height.equalTo(58);
            make.left.equalTo(self.scrollView.snp.left).offset(0);
            make.top.equalTo(self.weightInfoView.snp.bottom).offset(self.imageInfoViewHeight);
            make.bottom.equalTo(self.scrollView.snp.bottom).offset(0);
        }
    }
    func setOrderListModel(orderInfoModel:OrderListModel) {
        self.orderModel = orderInfoModel
        calculateTextV.textView.text = orderInfoModel.goodsNote;
        weightInfoView.orderInfoModel = orderInfoModel;
        calculateTextV.textView.text = orderInfoModel.goodsNote;
        infoProductV.productStr = orderInfoModel.goodsName;
    }
    func textFieldDidChange() {
        if (infoProductV.selectConditionModel != nil) && weightInfoView.weightTF.text?.count ?? 0 > 0 {
            confirmBtn.backgroundColor = UIColor.init(hexString: "#FFD10A")
        } else {
            confirmBtn.backgroundColor = UIColor.init(hexString: "#E3E3E3")
        }
    }
    @objc func confirmBtnAction() {
        self.view.endEditing(true)
        if (infoProductV.selectConditionModel == nil) {
            Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_goodsType_select))
            return
        } else if (weightInfoView.weightTF.text?.count ?? 0 <= 0) {
            Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_goodsMsg_required))
            return;
        }
        if (orderModel == nil) {
            orderModel = OrderListModel.init()
        }
        orderModel.goodsName = infoProductV.selectConditionModel.condition
        
        let lenghtValue = Double(weightInfoView.lenghtTF.text ?? "") ?? 0
        let wideValue = Double(weightInfoView.wideTF.text ?? "") ?? 0
        let heightValue = Double(weightInfoView.heightTF.text  ?? "") ?? 0
        let widthValue = Double(weightInfoView.weightTF .text ?? "") ?? 0

        orderModel.goodsWeight = Int(widthValue)
        orderModel.goodsLength = lenghtValue
        orderModel.goodsWidth = wideValue
        orderModel.goodsHeight = heightValue
        orderModel.goodsNote = calculateTextV.textView.text
        
        //zs待处理阿里云
        if ((informationBlock) != nil) {
            informationBlock();
        }
        self.navigationController?.popViewController(animated: true)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.view.endEditing(true)
    }
    
    lazy var weightInfoView: SpotInfoWeightView = {
        let weightInfoView:SpotInfoWeightView = Bundle.main.loadNibNamed("SpotInfoWeightView", owner: self, options: nil)?.first as! SpotInfoWeightView
        weightInfoView.spotInfoWeightBlick = {
            self.textFieldDidChange()
        }
        weightInfoView.setOrderInfoModel(orderInfoModel: orderModel)
        return weightInfoView
    }()
    lazy var imageInfoView: SpotEditInfoImageView = {
        let imageInfoView = SpotEditInfoImageView.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: imageInfoViewHeight), vc: self, isTitle: true, isBig: false)
        imageInfoView.supportValueViewBlock = {
            self.view.endEditing(true)
        }
        imageInfoView.supportClickImageBlock = { (ary) in
            let lookImageC:LookImageController = LookImageController.init()
            self.orderModel.imgDataDicAry = ary
            lookImageC.dataDicAry = ary
            self.navigationController?.pushViewController(lookImageC, animated: true)
        }
        if orderModel.imageIDAry.count > 0 {
//            imageInfoView.dataDicAry = orderModel.imageIDAry
        } else {
            if orderModel.goodsPic.count > 0 {
                var data:Array = Array<Dictionary<String, Any>>()
                /*
                for imgURlDic in orderModel.goodsPic {
                    data.append(["isUrl":"1","data":imgURlDic["objectKey"]])
                }
                orderModel.imgDataDicAry = data
 */
//                imageInfoView.dataDicAry = data
            }
        }
        return imageInfoView
    }()
    lazy var calculateTextV: SpotCalculateTextView = {
        let calculateTextV:SpotCalculateTextView = Bundle.main.loadNibNamed("SpotCalculateTextView", owner: self, options: nil)?.first as! SpotCalculateTextView
        calculateTextV.textMaxCount = 100
        calculateTextV.textView.text = orderModel.goodsNote
        return calculateTextV
    }()
    lazy var infoProductV: SpotInfoProductView = {
        let infoProductV:SpotInfoProductView = SpotInfoProductView.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: 190))
        infoProductV.spotInfoProductBlock = {
            self.textFieldDidChange()
        }
        infoProductV.setProductStr(productStr: orderModel.goodsName)
        return infoProductV
    }()
    
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView.init()
        scrollView.keyboardDismissMode = .onDrag
        return scrollView
    }()

    lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton.init(type: .custom)
        confirmBtn.layer.masksToBounds = true
        confirmBtn.layer.cornerRadius = 30
        confirmBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_confirm), for: .normal)
        confirmBtn.addTarget(self, action: #selector(confirmBtnAction), for: .touchUpInside)
        confirmBtn.backgroundColor = UIColor.init(hexString: "#E3E3E3")
        confirmBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        confirmBtn.setTitleColor(UIColor.init(hexString: "#616161"), for: .normal)
        return confirmBtn
    }()

}
